﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using Filter;
    using System;
    using System.Drawing;
    using System.Globalization;
    using System.Windows.Forms;        

    /// <summary>Filter specified properties form.</summary>
    public partial class ColorSegmentationForm : Form, IOptionsForm
    {
        private readonly ColorSegmentation filter;

        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="ColorSegmentationForm"/> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        public ColorSegmentationForm(Processor processor)
        {
            InitializeComponent();
            filter = AOF.Init(processor, this, gbProcess, 
                                new ColorSegmentation()) as ColorSegmentation;
        }

                /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            // grayscale example
            Bitmap b = filter.Apply(Processor.OriginalImage);
            string s = 
                Program.L.Text[40] + Program.L.Text[107] + ": " +
                Program.L.Text[115] + "=" + filter.NumberOfSegments + "; " +
                Program.L.Text[108] + "=" + filter.ColorSpace + "; " +
                Program.L.Text[111] + "=" + filter.ColorDistance + "; " +
                Program.L.Text[112] + "=" + filter.TraceRadius + "; " +
                Program.L.Text[113] + "=" + filter.MinNumberOfSegmentPixels + "; ";
            Processor.Change(b, s);
            lbSegmentNumbersValue.Text = filter.NumberOfSegments.ToString(
                CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[107]; // Name of filter
            tabctrlOptions.TabPages[0].Text = Program.L.Text[57]; // Options
            gbColorSpace.Text = Program.L.Text[108];
            chBoxProgressBar.Text = Program.L.Text[109];
            gbSegmentationOptions.Text = Program.L.Text[110];
            lbColorDistanceText.Text = Program.L.Text[111];
            lbTraceRadiusText.Text = Program.L.Text[112];
            lbMinNumberOfSegmentPixelsText.Text = Program.L.Text[113];
            gbResults.Text = Program.L.Text[114];
            lbMinNumberOfSegmentPixelsText.Text = Program.L.Text[115];
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {
            try
            {
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }

        private void ChBoxProgressBarCheckedChanged(object sender, EventArgs e)
        {
            filter.ShowProgressBar = chBoxProgressBar.Checked;
            // Processor.ApplyFilterRTP(); // filter not realtime-able
        }

        private void RdBtnLabCheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnLab.Checked)
            {
                filter.ColorSpace = ColorSpace.ColorSpaceEnum.LAB;
            }
        }

        private void RdBtnLuvCheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnLuv.Checked)
            {
                filter.ColorSpace = ColorSpace.ColorSpaceEnum.LUV;
            }
        }

        private void TrbarColorDistanceValueChanged(object sender, EventArgs e)
        {
            filter.ColorDistance = trbarColorDistance.Value / 100.0f;
            lbColorDistanceValue.Text = filter.ColorDistance.ToString(
                CultureInfo.InvariantCulture);
        }

        private void TrBarTraceRadiusValueChanged(object sender, EventArgs e)
        {
            filter.TraceRadius = (byte)trBarTraceRadius.Value;
            lbTraceRadiusValue.Text = filter.TraceRadius.ToString(
                CultureInfo.InvariantCulture);
        }

        private void TrBarMinNumberOfSegmentPixelsValueChanged(object sender, EventArgs e)
        {
            filter.MinNumberOfSegmentPixels = trBarMinNumberOfSegmentPixels.Value;
            lbMinNumberOfSegmentPixelsValue.Text = 
                filter.MinNumberOfSegmentPixels.ToString(
                CultureInfo.InvariantCulture);
        }
    }
}
